﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Fabrica
{
    public partial class AgregarJuguete : Form
    {
        List<ToyUseMaterial> lista = new List<ToyUseMaterial>();
        FabricaEntities entities = new FabricaEntities();
        VentaNodo1.VentaEntities entitiesVenta = new VentaNodo1.VentaEntities();
        VentaNodo2.VentaEntities1 entitiesVenta2 = new VentaNodo2.VentaEntities1();

        public AgregarJuguete()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            lista.Add(new ToyUseMaterial
            {
                materialId = (from m in entities.Materials
                              where textBox6.Text == m.name
                              select m).FirstOrDefault().materialId,
                quantity = Convert.ToInt32(textBox5.Text)
            });
            dataGridView1.Update();
            
            MessageBox.Show("Material Agregado con exito");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int theToyId = (from t in entities.Toys
                            select t).Count() + 1;
            for (int i = 0; i < lista.Count; i++)
                lista[i].toyId = theToyId;
            entities.AddToToys(new Toy
            {
                toyId = theToyId,
                price = Convert.ToDecimal(textBox3.Text),
                name = textBox1.Text,
                description = textBox2.Text
            });
            entitiesVenta.AddToToys(new VentaNodo1.Toy
            {
                toyid = theToyId,
                price = Convert.ToInt32(textBox3.Text),
                name = textBox1.Text,
                description = textBox2.Text
            });
            entitiesVenta2.AddToToys(new VentaNodo2.Toy
            {
                toyid = theToyId,
                price = Convert.ToInt32(textBox3.Text),
                name = textBox1.Text,
                description = textBox2.Text
            });
            entities.SaveChanges();
            entitiesVenta.SaveChanges();
            entitiesVenta2.SaveChanges();
            
            foreach (ToyUseMaterial item in lista)
            {
                entities.AddToToyUseMaterials(item);
            }
            entities.SaveChanges();
            MessageBox.Show("Juguete Agregado con exito");
        }


    }
}
